package userdisplays;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class EditableImagePanel extends JPanel implements Editable,
        MouseListener, Serializable
{
    private static final long serialVersionUID = 1L;
    private static final Font font = new Font("Arial", Font.PLAIN, 10);
    private static final int WIDTH = 65, HEIGHT = 65;
    private UserPanel panel;
    private ImageIcon icon;
    private boolean editable = true;

    public EditableImagePanel(UserPanel p)
    {
        super();
        panel = p;
        setPreferredSize(new Dimension(WIDTH + 8, HEIGHT + 8));
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent arg0)
    {
        if (editable && arg0.getClickCount() == 2)
        {
            final JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
                String filename = fc.getSelectedFile().getAbsolutePath();
                String ext = filename.substring(filename.lastIndexOf('.'))
                        .toLowerCase();
                if (ext.equals(".gif") || ext.equals(".jpg")
                        || ext.equals(".png"))
                {
                    Image image = Toolkit.getDefaultToolkit()
                            .getImage(filename);
                    Image scaledImage = image.getScaledInstance(WIDTH, HEIGHT,
                            Image.SCALE_SMOOTH);
                    icon = new ImageIcon(scaledImage);
                }
                else
                {
                    JOptionPane.showMessageDialog(null,
                            "File could not be opened as an image",
                            "File not read", JOptionPane.ERROR_MESSAGE);
                }
                repaint();
                panel.sendPanel();
            }
        }
    }

    public void mouseEntered(MouseEvent arg0)
    {
    }

    public void mouseExited(MouseEvent arg0)
    {
    }

    public void mousePressed(MouseEvent arg0)
    {
    }

    public void mouseReleased(MouseEvent arg0)
    {
    }

    public void lock()
    {
        editable = false;
    }

    public void unlock()
    {
        editable = true;
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, WIDTH + 7, HEIGHT + 7);
        g.drawRect(1, 1, WIDTH + 5, HEIGHT + 5);
        g.setColor(Color.WHITE);
        g.drawRect(2, 2, WIDTH + 3, HEIGHT + 3);
        g.setColor(Color.BLACK);
        g.drawRect(3, 3, WIDTH + 1, HEIGHT + 1);
        if (icon != null)
        {
            g.drawImage(icon.getImage(), 4, 4, this);
        }
        else if(editable)
        {
            g.setFont(font);
            g.drawString("Double-Click", 6, HEIGHT / 2);
            g.drawString("to Set Image", 6, HEIGHT / 2 + 12);
        }
    }
}
